﻿using System;
using System.Drawing;
using System.Windows.Forms;
using DemoSDKForWinForm.Properties;
using Microsoft.Win32;

namespace DemoSDKForWinForm
{
    public class Icons : IDisposable
    {
        /// <summary>
        /// 图标列表集合
        /// </summary>
        public ImageList ImageList { get; set; }

        /// <summary>
        /// 得到图标个数
        /// </summary>
        public int Count
        {
            get { return ImageList.Images.Count; }
        }

        private bool _disposed = false;

        public Icons()
            : base()
        {
            this.ImageList = new ImageList();
            this.ImageList.ColorDepth = ColorDepth.Depth32Bit;
            this.ImageList.ImageSize = SystemInformation.SmallIconSize; //设置与系统相同的小图标图片
            this.ImageList.ImageSize = new Size(16, 17);

            this.ImageList.Images.Add("unknow", Resources.unknow);
            this.ImageList.Images.Add("folder", Resources.folder);
            this.ImageList.Images.Add("dir", Resources.dir);
        }

        private void CleanUp(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    ImageList.Dispose();
                }
            }
            _disposed = true;
        }

        public void Dispose()
        {
            CleanUp(true);
            GC.SuppressFinalize(this);
        }

        ~Icons()
        {
            CleanUp(false);
        }

        public int GetIconIndex(string fileName, bool isDirectory)
        {
            string ext = Icons.GetPostfixStr(fileName); //文件的后缀名 .jpg
            ext = ext.Trim('.');
            if (string.IsNullOrEmpty(ext)) //文件夹 或者没有相关联的后缀名
            {
                if (isDirectory) //文件夹
                {
                    ext = "folder"; // for directories
                }
                else
                {
                    ext = "unknow"; // for files without extension
                }
            }

            if (this.ImageList.Images.ContainsKey(ext))
            {
                return this.ImageList.Images.IndexOfKey(ext);
            }
            else
            {
                object obj = Resources.ResourceManager.GetObject(ext, Resources.Culture);
                if (obj != null)
                {
                    this.ImageList.Images.Add(ext, ((System.Drawing.Bitmap)(obj)));
                    return this.Count - 1;
                }
                else
                {
                    return 0;
                }
            }
        }

        public static string GetPostfixStr(string filename)
        {
            if (filename.IndexOf(".") == -1)
            {
                return string.Empty;
            }
            int start = filename.LastIndexOf(".");
            int length = filename.Length;
            string postfix = filename.Substring(start, length - start);
            return postfix;
        }

        /// <summary>
        /// 通过扩展名得到描述
        /// </summary>
        /// <param name="fileName"></param>
        public static string GetExtsDescription(string fileName)
        {
            string ext = Icons.GetPostfixStr(fileName);

            //从注册表中读取扩展名相应的子键  
            RegistryKey extsubkey = Registry.ClassesRoot.OpenSubKey(ext);
            if (extsubkey == null)  //没有找到
            {
                return ext.ToUpper().Substring(1) + "文件";
            }

            //取出扩展名对应的文件类型名称  
            string extdefaultvalue = extsubkey.GetValue(null) as string;
            if (extdefaultvalue == null)
            {
                return string.Empty;
            }

            //扩展名类型是可执行文件
            if (extdefaultvalue.Equals("exefile", StringComparison.InvariantCultureIgnoreCase))
            {
                //从注册表中读取文件类型名称的相应子键  
                RegistryKey exefilesubkey = Registry.ClassesRoot.OpenSubKey(extdefaultvalue);
                if (exefilesubkey != null)  //如果不为空
                {
                    string exefiledescription = exefilesubkey.GetValue(null) as string;   //得到exefile描述字符串  
                    if (exefiledescription != null)
                    {
                        return exefiledescription;
                    }

                }
                return string.Empty;
            }


            //从注册表中读取文件类型名称的相应子键  
            RegistryKey typesubkey = Registry.ClassesRoot.OpenSubKey(extdefaultvalue);
            if (typesubkey == null)
            {
                return string.Empty;
            }

            //得到类型描述字符串  
            string typedescription = typesubkey.GetValue(null) as string;
            if (typedescription != null)
            {
                return typedescription;
            }
            return string.Empty;
        }
    }
}
